import java.util.Scanner;

/**
 * Demo class for use as part of the CSES Svn Workshop.
 *
 * @author Chris Moghbel (cmoghbel@ucsd.edu)
 */
public class SvnWorkshopDemo {

  /**
   * Returns a count of the number of times each letter in the alphabet occurs
   * in the string.
   *
   * @param source - The original string.
   *
   * @return int[] - An array where the first slot holds the number of times a
   * occurs, the second slot holds the number of times b occurs, etc., up to the
   * 26th slot for z. 
   */
  public static int[] characters(String source) {
    // TODO: Implement this method.
    // HINT: You can convert between characters and numbers like this:
    // char c = (char) 'a'+3 // c is 'd'
    // boolean b = (c >= 'a') // b is true
    for(char val = 'a'; val <= 'z'; val++)
    {
    } 
    return new int[26];
  }

  /**
   * Takes in a string, and returns the number of words in it. Depending on how
   * detailed you want to be, you can count all words, or just unique words. You
   * can also decide whether or not you want to include punctuation as part of a
   * word, or not.
   *
   * @param source - The original string.
   *
   * @return int - The number of words in the string. 
   */ 
  public static int words(String source) {
    // TODO: Implement this method.
    // HINT: Look up String.split() or StringTokenizer.
    return 1;
  }

  /**
   * Takes in a source string and a word, and returns the number of times that
   * word occurs in that string.
   *
   * @param source - The source string.
   * @param word - The word to get the frequency for.
   *
   * @return int - The number of times the given word occurs in the given
   * string.
   */
  public static int frequency(String source, String word) {
    // TODO: Implement this method.
    // HINT: Look up String.split() or StringTokenizer.
    StringTokenizer st = new StringTokenizer (source);
    while(st.hasNextToken)
    {
        
    }
    return 1;
  }

  /**
   * Returns the reverse of a string.
   *
   * @param source - The original string.
   *
   * @return String - The reverse of the original string.
   */
  public static String reverse(String source) {
    // TODO: Implement this method.
    return reverse;
  }

  /**
   * Returns the longest word in a string.
   *
   * @param source - The original string.
   *
   * @return String - The longest word in the string.
   */
  public static String longest(String source) {
    // TODO: Implement this method.
    // HINT: Look up String.split() or StringTokenizer.
    return "";
  }

  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    String line = "";
    while (!"exit".equals(line)) {
      System.out.print("Enter choice (f, w, c, r, l): ");
      line = scanner.nextLine();  
      if ("exit".equals(line)){
        System.out.println("Thank you for using this application!");
        System.exit(0);
      }
      System.out.print("Enter sentence: ");
      String source = scanner.nextLine();
      if (line.startsWith("f")) {
        System.out.print("Enter word: ");
        String word = scanner.nextLine();
        System.out.println(word + " occurs " + frequency(source, word)
          + " times in the string.");
      }
      else if (line.startsWith("w")) {
        System.out.println("The number of words in string is " + words(source));
      }
      else if (line.startsWith("c")) {
        System.out.println("The character frequencies of the string are: ");
        int[] frequencies = characters(source);
        if (frequencies.length != 26)
            System.err.println ("Error: characters() returned invalid array.");
        else {
            for (int i=0; i<26; i++) {
                System.out.println ((char)('A'+i) + ": " + frequencies[i]);
            }
        }
      }
      else if (line.startsWith("r")) {
        System.out.println(reverse(source));
      }
      else if (line.startsWith("l")) {
        System.out.println("The longest word in the string is ["
          + longest(source) + "].");
      }
      else {
        System.out.println("Incorrent choice. Choose f (frequency) or w "
          + "(word count).");
      }
    }
  }
}
